/**
 * @file index.js
 * @author admin
 * @description 信息绑定页面
 */
var app = getApp();
Page({
  onLoad (){
    var userInfo = wx.getStorageSync('userInfo');
    this.data.name = userInfo.name;
    this.data.mobile = userInfo.mobile;
  },
  doBind () {
    var name, mobile,openid,code; 

    name = this.data.name;
    mobile = this.data.mobile;
    if(name === '') {
      wx.showToast({
        title: '姓名不能为空',
        image: '../../images/failmsg.png',
        duration: 2000
      });
      return;
    };
    if(mobile == '') {
      wx.showToast({
        title: '联系方式不能为空',
        image: '../../images/failmsg.png',
        duration: 2000
      });
      return;
    };
    wx.request({
      url: app.globalData.domain + '/userInfo/bindUser', //仅为示例，并非真实的接口地址
      data: {
        openid: wx.getStorageSync('user').openid,
        name: name,
        mobile: mobile
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        var data = res.data;
        if(data.code == 200) {
          var userInfo = data.data;
          console.log(userInfo);
          wx.setStorageSync('userInfo', userInfo);
          wx.navigateTo({
            url: "../index/index"
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
            duration: 1000     
          }) 
        }
        
      },
      error (res) {
        wx.showToast({
          title: '服务调用失败',
          icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
          duration: 1000     
        }) 
      }
    })
    wx.switchTab({
      url: '../index/index'
    })
  },
  inputName (e) {
    this.setData({
        name: e.detail.value
      });
  },
  inputMobile (e) {
    this.setData({
        mobile: e.detail.value
      });
  }
})